﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData.ModelProviders;
using NHibernate.Mapping;

namespace HibernateDynamicData.Adapter
{
    public class NHibernateTableProvider : TableProvider
    {
        public NHibernateTableProvider(NHibernateDataModelProvider dataModel, PersistentClass classMapping)
            : base(dataModel)
        {
            this.Name = classMapping.Table.Name;
            this.EntityType = classMapping.MappedClass;
            foreach (var property in classMapping.PropertyIterator)
            {
                var column = property.ColumnIterator.OfType<Column>().FirstOrDefault();
                if (column == null)
                    continue;
                var col = new NHibernateColumnProvider(this, property, column);
                this.columns.Add(col);
            }
        }

        private List<ColumnProvider> columns = new List<ColumnProvider>();
        public override System.Collections.ObjectModel.ReadOnlyCollection<ColumnProvider> Columns
        {
            get { return columns.AsReadOnly(); }
        }

        public override IQueryable GetQuery(object context)
        {
            throw new NotImplementedException();
        }
    }
}
